package foliotracker.GUI;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import foliotracker.controllers.BuyStockController;
import foliotracker.views.IBuyStockView;

public class BuyStockDialog extends DialogBase implements IBuyStockView {

	private static final long serialVersionUID = 1L;
	private JComboBox symbolList;
	private BuyStockController controller;
	private JLabel symbolError, quantityError, costError, dateError;
	private JTextField amount, cost, date;

	public BuyStockDialog(BuyStockController controller){
		super("Buy Stock");
		panel.setMinimumSize(new Dimension(500,700));
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		this.controller = controller;
		
		JLabel stockLabel = new JLabel("Symbol");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(stockLabel, c);
		
		symbolList = new JComboBox(controller.getSymbols().toArray());
		symbolList.setEditable(true);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(symbolList, c);
		
		symbolError = new JLabel();
		symbolError.setForeground(Color.RED);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		panel.add(symbolError, c);
	
		JLabel amountLabel = new JLabel("Amount");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(amountLabel, c);
	
		
		amount = new JTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 2;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(amount, c);
		
		quantityError = new JLabel();
		quantityError.setForeground(Color.RED);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		panel.add(quantityError, c);
		
		JLabel costLabel = new JLabel("Cost");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 4;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(costLabel,c);
		
		cost = new JTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 4;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(cost,c);
		
		costError = new JLabel();
		costError.setForeground(Color.RED);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 5;
		c.gridwidth = 2;
		panel.add(costError,c);
		
		JLabel dateLabel = new JLabel("Date");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 6;
		c.weightx = 1.0;
		c.weighty = 1.0; 
		panel.add(dateLabel, c);
		
		date = new JTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 6;
		c.weightx = 1.0;
		c.weighty = 1.0;
		panel.add(date, c);
		
		dateError = new JLabel();
		dateError.setForeground(Color.RED);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 7;
		c.gridwidth = 2;
		panel.add(dateError, c);
		
		 
		 this.setSize(250, 250);
		 setPosition();
 
	}
	
	@Override
	protected void okButton_Clicked() 
	{
		final BuyStockDialog dialog = this;
		okButton.setEnabled(false);
		
		EventQueue.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				if (controller.validate() == true)
				{
					controller.buyStock();
					dialog.setVisible(false);
				}
				else
				{
					okButton.setEnabled(true);
				}
			}
		});
	}
	
	@Override
	protected void cancelButton_Clicked() {
		this.setVisible(false);
		
	}

	@Override
	public String getSymbol(){
		return symbolList.getSelectedItem().toString();
	}

	@Override
	public void setSymbolError(String value) {
		symbolError.setText(value);
	}

	@Override
	public String getDateField() {
		return date.getText();
	}

	@Override
	public void setDateError(String value) {
		dateError.setText(value);
	}

	@Override
	public String getQuantityField() {
		return amount.getText();
	}

	@Override
	public void setQuantityError(String value) {
		quantityError.setText(value);
	}

	@Override
	public String getCostField() {
		return cost.getText();
	}

	@Override
	public void setCostError(String value) {
		costError.setText(value);
	}

}
